package model.menupages;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

import model.Gamefield;
import view.Master;
import view.menuelements.MenuButton;
import view.menuelements.MenuLabel;
import view.menuelements.Menupanel;
import view.steine.Spielblock;
import view.steine.Standardblock;
import controller.Gamefieldcontroller;
import controller.Heytetrisapplication;
import controller.Highscorecontroller;

public class Gamepage extends JFrame {

	private int countdown = 3;
	public Menupanel centerpanel = new Menupanel();
	public MenuLabel counter = new MenuLabel("" + countdown);
	public MenuLabel stones[] = new MenuLabel[Gamefield.getBreite()
			* Gamefield.getHoehe()];
	private Timer t;
	public JLabel hg;
	private Gamepage gamepage = this;
	private Menupanel eastpanel = new Menupanel();
	private Menupanel westpanel = new Menupanel();
	public MenuLabel rows = new MenuLabel("<html>Rows<br>0</html>");
	public MenuLabel score = new MenuLabel("<html>Score<br>0</html>");
	public MenuLabel timer = new MenuLabel("<html>Time<br>0:00</html>");
	public MenuLabel level = new MenuLabel("<html>Level<br>1</html>");
	public MenuLabel highscore = new MenuLabel("");
	public MenuButton pause = new MenuButton("||", 50, 50);
	int secs = 0;
	int mins = 0;

	public Gamepage() throws HeadlessException {
		super();
		// setzt Standardoptionen und den Schließenbutton
		Master.setStandardpageOptions(this);
		Master.addCloseButton(this);

		// kein Layoutmanager, da die Komponenten manuell zugeordnet werden
		centerpanel.setLayout(null);
		eastpanel.setLayout(new BoxLayout(eastpanel, BoxLayout.Y_AXIS));
		westpanel.setLayout(new BoxLayout(westpanel, BoxLayout.Y_AXIS));

		// formatiert den Countdown
		counter.setFont(new Font(Font.DIALOG, Font.BOLD, 220));
		counter.setForeground(Color.red);

		// lädt hg bild
		URL ressource1 = ClassLoader.getSystemResource("Spielfeld.png");
		Icon img = new ImageIcon(ressource1);
		hg = new JLabel(img);

		// positionieren des HG bildes / des Textes
		// 0,4 der Bildschirmbreite und 0,1 der Hoehe des Bildschirms fuer obere
		// linke Ecke
		// Spielfeld hat die Groesse 240*480 Pixel (1 Stein 24 Pixel²)
		// Countdown hat die Breite von 244 (bei Schriftgroesse 220) und eine
		// Hoehe von 175 px
		// Position vom Countdown skaliert mit der Bildschirmhoehe
		hg.setBounds(getWidth() * 1 / 4, getHeight() / 10,
				24 * Gamefield.getBreite(), 24 * Gamefield.getHoehe());
		counter.setBounds(0, hg.getHeight() / 4, 244, 175);

		// Hinzufuegen der Komponenten
		for (int i = 0; i < stones.length; i++) {
			stones[i] = new MenuLabel("");
			stones[i].setBounds(0, 0, 24, 24);
			hg.add(stones[i]);
		}
		hg.add(counter);
		centerpanel.add(hg);
		add(centerpanel, BorderLayout.CENTER);

		// Zaehler hinzufuegen
		Dimension dimension = new Dimension(200, 150);
		score.setPreferredSize(dimension);
		score.setMinimumSize(dimension);
		score.setMaximumSize(dimension);
		eastpanel.add(score);
		rows.setPreferredSize(dimension);
		rows.setMinimumSize(dimension);
		rows.setMaximumSize(dimension);
		eastpanel.add(rows);
		level.setPreferredSize(dimension);
		level.setMinimumSize(dimension);
		level.setMaximumSize(dimension);
		eastpanel.add(level);
		timer.setPreferredSize(dimension);
		timer.setMinimumSize(dimension);
		timer.setMaximumSize(dimension);
		eastpanel.add(timer);
		add(eastpanel, BorderLayout.EAST);

		// Highscore hinzufuegen
		Highscorecontroller hs = new Highscorecontroller();
		Dimension dimension2 = new Dimension(150, 350);
		highscore.setText("<html>Highscore<br>" + hs.getScore(0) + "</html>");
		highscore.setPreferredSize(dimension2);
		highscore.setMinimumSize(dimension2);
		highscore.setMaximumSize(dimension2);
		Menupanel bp = new Menupanel();
		bp.add(pause);
		bp.setPreferredSize(dimension2);
		bp.setMinimumSize(dimension2);
		bp.setMaximumSize(dimension2);

		pause.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Heytetrisapplication.gamepaused();
			}
		});

		westpanel.add(bp);
		westpanel.add(highscore);
		add(westpanel, BorderLayout.WEST);

		// startet den Countdown / evtl umbenennen
		Master.setPageVisible(this);
		startCountdown();
	}

	private void startCountdown() {
		t = new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				counter.setText("" + --countdown);
				if (countdown == 0) {
					counter.setText("");
					t.stop();
					inittimer();
					// KeyListener
					gamepage.addKeyListener(new KeyAdapter() {

						@Override
						public void keyPressed(KeyEvent e) {
							switch (e.getKeyCode()) {
							case KeyEvent.VK_UP:
							case KeyEvent.VK_W:
								Heytetrisapplication.drehen();
								break;
							case KeyEvent.VK_LEFT:
							case KeyEvent.VK_A:
								Heytetrisapplication
										.steinbewegen(Spielblock.LEFT);
								break;
							case KeyEvent.VK_RIGHT:
							case KeyEvent.VK_D:
								Heytetrisapplication
										.steinbewegen(Spielblock.RIGHT);
								break;
							case KeyEvent.VK_ESCAPE:
								Heytetrisapplication.gamepaused();
								break;
							case KeyEvent.VK_DOWN:
							case KeyEvent.VK_S:
								if(!Gamefieldcontroller.speed){
								Gamefieldcontroller.speedUp();
								}
								break;

							}
						}

						@Override
						public void keyReleased(KeyEvent e) {
							switch (e.getKeyCode()) {
							case KeyEvent.VK_DOWN:
							case KeyEvent.VK_S:
								Gamefieldcontroller.slowDown();
								break;
							}
						}

					});
					Heytetrisapplication.gamestarted(gamepage);
				}

			}

		});
		t.start();
	}

	private void inittimer() {
		Timer s = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (secs < 59) {
					secs++;
				} else {
					secs = 0;
					mins++;
				}
				if (secs < 10) {
					timer.setText("<html>Time<br>" + mins + ":0" + secs
							+ "</html>");
				} else {
					timer.setText("<html>Time<br>" + mins + ":" + secs
							+ "</html>");
				}
			}
		});
		s.start();

	}

	public void steinAnzeigen(Standardblock sb, int x, int y) {
		for (int i = 0; i < stones.length; i++) {
			if (stones[i].getIcon() == null) {
				stones[i].setBounds(x, y, 24, 24);
				stones[i].setIcon(sb.getBlock());
				break;
			}
		}
		repaint();
		validate();
	}

	public void steinVerschwinden(Standardblock sb, double xkor, double ykor) {
		for (int i = 0; i < stones.length; i++) {
			if (stones[i].getBounds().getX() == xkor
					&& stones[i].getBounds().getY() == ykor) {
				stones[i].setBounds(1000, 1000, 24, 24);
				stones[i].setIcon(null);
				break;
			}
		}
		repaint();
		validate();
	}

	public void deleteAllStones() {
		for (int i = 0; i < stones.length; i++) {
			stones[i].setBounds(1000, 1000, 24, 24);
			stones[i].setIcon(null);
		}
	}

	public void drawAllStones(Standardblock[][] vorhandeneSpielsteine) {
		for (int y = 0; y < Gamefield.getHoehe(); y++) {
			for (int x = 0; x < Gamefield.getBreite(); x++) {
				if (vorhandeneSpielsteine[x][y] != null) {
					vorhandeneSpielsteine[x][y].draw(x, y + 1, this);
				}
			}
		}

	}
}
